<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\User as UserModel;
use sand\handle;
use app\admin\controller\Base;
//use think\Loader;
/**
* 
*/
class User extends Base
{
    public function index(){
        $search=input('get.search');
        $map=[];
        if($search) $map=['name|phone'=>$search];
    	$users=model('user')->where($map)->paginate(10);
    	$this->assign('users',$users);
        return $this->fetch();

    }
    public function add(){
    	return $this->fetch();
    }

    public function doadd(){
    	$user=[
    		'name'=>$_POST['name'],
            'idcard'=>$_POST['idcard'],
    		'phone'=>$_POST['phone']
    	];
    	$validate=validate('User');
    	if(!$validate->check($user))
    		$this->error($validate->getError());
        $params = require_once CONF_PATH.'sand/config/params.php';
        $cert = require_once CONF_PATH.'sand/config/cert.php';
        $idCardVerify = $params['IdCardVerify'];
        $idCardVerify['pt']['name'] =$user['name'];
        $idCardVerify['pt']['certType'] ="0101";
        $idCardVerify['pt']['certNo'] =$user['idcard'];
        $idCardVerify['pt']['returnPic'] =0;
        $handle = new handle($cert);
        //dump($idCardVerify);exit();
        $res=json_decode($handle->execute($idCardVerify));
        if($res->respCode=="0000"){
            if($res->validateStatus=="0"){
                $userModel = new UserModel($user);
                 if(!$userModel->save())
                    $this->error("用户添加失败");
                else $this->success("用户添加成功");
            }else{
                $this->error("验证不通过");
            }
        }else $this->error($res->respDesc);
    }
}